// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

// 方形转盘
interface IAwardRectTurnTable {
    position?: cc.Vec2;
    desc?: string;
    prob?: number;
}
const FIRST_TURN_COUNT = 3; // 开始转圈数

const {ccclass, property} = cc._decorator;

@ccclass
export default class Main extends cc.Component {

    @property(cc.Node)
    nodeImgSelect: cc.Node = null;

    @property(cc.Node)
    nodeWinner1: cc.Node = null;

    @property(cc.Node)
    nodeWinner2: cc.Node = null;

    // 方形转盘
    private _awards: IAwardRectTurnTable[] = [];
    private _awardIndex: number = 0;
    private _isClickSelect: boolean = false;

    private _initFrame: number = 0;
    private _frameLimit: number = 7;
    private _turnCount: number = 1; // 当前转圈数量

    private _selectAwardIndex: number = 0; // 抽中的奖品

    onLoad () {
        console.log("onLoad");
        this.initData();
        this.initAction();
    }

    private initData(): void {
        this._awards = [
            {position: cc.v2(-196, 171), desc: "谢谢参与", prob: 15},
            {position: cc.v2(0, 171), desc: "iphone 11", prob: 2},
            {position: cc.v2(196, 171), desc: "20G流量卡", prob: 15},
            {position: cc.v2(196, 0), desc: "50元红包", prob: 8},
            {position: cc.v2(196, -171), desc: "谢谢参与", prob: 15},
            {position: cc.v2(0, -171), desc: "5元红包", prob: 25},
            {position: cc.v2(-196, -171), desc: "30元花费", prob: 12},
            {position: cc.v2(-196, 0), desc: "50元红包", prob: 8}
        ];
        this._awardIndex = 0;
    }

    private initAction() {
        this.actAwardWinnerFunc();
    }

    private clickLottery(sender, str) {
        console.log("clickLottery", sender, str);
        if (this._isClickSelect) {
            cc.log("now in turn~")
            return;
        }

        // init data
        this._selectAwardIndex = this.getLotteryRanomIndex();
        cc.log("当前抽中的是:" + this._awards[this._selectAwardIndex].desc + ",索引:" + this._selectAwardIndex)

        this._awardIndex = 0;
        this._frameLimit = 7;
        this._turnCount = 1; // 当前转圈数量

        this._isClickSelect = true;
    }

    private getLotteryRanomIndex(): number {
        let tempRandNum = Math.floor(Math.random() * 100);

        let chance = 0;
        for (let i = 0; i < this._awards.length; i++) {
            chance += this._awards[i].prob;
            if (chance >= tempRandNum) {
                return i;
            }
        }
    }

    private actAwardWinnerFunc(): void {
        this.nodeWinner1.getComponent(cc.Label).string = "恭喜玩家1xxx234抽中50元红包";
        this.nodeWinner2.getComponent(cc.Label).string = "恭喜1xxx567抽中20G流量卡";

        let posX = this.nodeWinner2.x;
        this.nodeWinner1.y = 0;
        this.nodeWinner2.y = -50

        // winner1 动作
        {
            let act1 = cc.moveTo(0.5, cc.v2(posX, 50));
            let act2 = cc.delayTime(1);
            let act3 = cc.callFunc(()=>{
                this.nodeWinner1.setPosition(posX, -50);

                // player name
                let playerName = ""
                let playerIndex = Math.floor(Math.random() * 1000);
                if (playerIndex < 10) {
                    playerName = "00" + playerIndex.toString();
                } else if (playerIndex < 100) {
                    playerName = "0" + playerIndex.toString();
                } else {
                    playerName = playerIndex.toString();
                }

                // player award
                let tempIndex = Math.floor(Math.random() * this._awards.length);
                let awardName = this._awards[tempIndex].desc;

                this.nodeWinner1.getComponent(cc.Label).string = "恭喜玩家1xxx" + playerName + "抽中" + awardName;                                
            }, this);
            let act4 = cc.moveTo(0.5, cc.v2(posX, 0));
            let act5 = cc.delayTime(1);
            let sequence = cc.sequence(act1, act2, act3, act4, act5);
            this.nodeWinner1.runAction(cc.repeatForever(sequence));
        }

        // winner2 动作
        {
            let act1 = cc.moveTo(0.5, cc.v2(posX, 0));
            let act2 = cc.delayTime(1);
            let act3 = cc.moveTo(0.5, cc.v2(posX, 50));
            let act4 = cc.delayTime(1);
            let act5 = cc.callFunc(()=>{
                this.nodeWinner2.setPosition(posX, -50);

                // player name
                let playerName = ""
                let playerIndex = Math.floor(Math.random() * 1000);
                if (playerIndex < 10) {
                    playerName = "00" + playerIndex;
                } else if (playerIndex < 100) {
                    playerName = "0" + playerIndex;
                } else {
                    playerName = playerIndex.toString();
                }

                // player award
                let tempIndex = Math.floor(Math.random() * this._awards.length);
                let awardName = this._awards[tempIndex].desc;

                this.nodeWinner2.getComponent(cc.Label).string = "恭喜玩家1xxx" + playerName + "抽中" + awardName;
            }, this);
            let sequence = cc.sequence(act1, act2, act3, act4, act5);
            this.nodeWinner2.runAction(cc.repeatForever(sequence));
        }
    }

    start () {
        console.log("start");
    }

    update (dt) {
        if (this._isClickSelect) {
            this._initFrame++;
            // n帧后执行一次
            if (this._initFrame >= this._frameLimit) {
                this._initFrame = 0;

                this._awardIndex++;
                if (this._awardIndex >= this._awards.length) {
                    this._awardIndex = 0;
                    this._turnCount++;

                    // 第二圈后速度减慢
                    if (this._turnCount >= 2) {
                        this._frameLimit++
                    } 
                }

                // 第三圈速度更慢
                if (this._turnCount == FIRST_TURN_COUNT) {
                    this._frameLimit = 20;
                }

                // 第四圈进行选择
                if (this._turnCount == FIRST_TURN_COUNT + 1) {
                    if (this._awardIndex == this._selectAwardIndex) {
                        this._isClickSelect = false;
                    }
                }

                this.nodeImgSelect.setPosition(this._awards[this._awardIndex].position);
            }
        }
    }
}
